//
//  RatView.m
//  UHealth
//
//  Created by gjtxz on 14/12/29.
//  Copyright (c) 2014年 gjtx. All rights reserved.
//

#import "RatView.h"
#import "NSString+BloodSugar.h"

#define kHeight                 self.frame.size.height
#define kJX                     (self.frame.size.width - 80 - (7*kHeight))/7.0
#define kOtherWhiteBlueColor    [UIColor colorWithRed:30.0/255.0 green:157.0/255.0 blue:254.0/255.0 alpha:1.f]

@implementation RatView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self DrawViewWithFrame:frame];
    }
    return self;
}

-(void)DrawViewWithFrame:(CGRect)frame
{
    for (int i = 0 ; i < 8; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.layer.masksToBounds = YES;
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    [self refreshViewWithCount:0 andDate:[NSDate date]];
}

-(void)refreshViewWithCount:(NSInteger)count andDate:(NSDate *)date
{
    if (count <= 0) {
        count = 0;
    }
    if (count > 7) {
        count = 7;
    }
    int i = 0;
    for (UIButton *button in self.subviews) {
        if (i == count) {
            button.enabled = YES;
            button.frame = CGRectMake(i*(kJX+kHeight), 0, 80, kHeight);
            [button.layer setCornerRadius:10];
             NSString *str = [NSString stringFromDate:date];
            [button setTitle:[NSString stringWithFormat:@"%@",[str substringToIndex:10]] forState:UIControlStateNormal];
            button.backgroundColor = kWhiteBlueColor;
        }else {
            if(i > count){
                if (i > _Count) {
                    button.enabled = NO;
                    button.backgroundColor = kOtherWhiteBlueColor;
                }
                button.frame = CGRectMake(i*(kJX+kHeight)+80-kHeight, 0, kHeight, kHeight);
            }else{
                button.enabled = YES;
                button.frame = CGRectMake(i*(kJX+kHeight), 0, kHeight, kHeight);
                button.backgroundColor = kWhiteBlueColor;
            }
            [button.layer setCornerRadius:CGRectGetHeight([button bounds]) / 2];
            [button setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        }
        i++;
    }
}


-(void)BtnClick:(UIButton *)Btn
{
    if (Btn.tag > _Count) {
        kDebug(@"超出天数 %d",(int)Btn.tag+1);
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(RatViewBtnClick:)]) {
        [_delegate RatViewBtnClick:Btn];
    }
}

@end
